package cn.tedu.utils;

import java.sql.*;

/**
 * 数据库连接工具类
 *
 */
public class DBUtils {
    // 加载驱动：代码块
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取数据库链接
     * @return 数据库链接
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        String url = "jdbc:mysql://localhost:3306/dang?characterEncoding=UTF-8&useSSL=false";
        String uname = "root";
        String pwd = "root";
        Connection connection = DriverManager.getConnection(url, uname, pwd);
        return connection;
    }

    /**
     * 关闭资源
     * @param resultSet 结果集
     * @param statement statement
     * @param connection 链接
     * @throws SQLException
     */
    public static void close(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {

        if (resultSet != null) {
            resultSet.close();
        }

        if (statement != null) {
            statement.close();
        }

        if (connection != null) {
            connection.close();
        }
    }

}
